#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int g_val = 100;

int main()
{
  if(!fork())
  {
    //child
    
    int cnt = 5;
    while(cnt){
      printf("I am child:  count: %d,g_val = %d,&g_val = %p\n",cnt,g_val,&g_val);
      --cnt;
      sleep(1);
      if(cnt == 3)
      {
         printf("################ child 更 改 数 据 ########################\n");
         g_val = 200;
         printf("################ child 更 改 数 据 完 成 ##################\n");
      }
    }
  }
  else{
    //parent
    while(1){
      printf("I am parent: g_val = %d,&g_val = %p\n",g_val,&g_val);
      sleep(1);
    } 
  }
  return 0;
}














/*int g_val1;  //未初始化
int g_val2 = 100;  //已初始化

//int main(int argc,char* argv[],char* env[])
int main()
{
  const char* s = "hhh";
  //每个区找个代表
  printf("code addr: %p\n",main);   //main函数典型的代码区代表
  printf("string addr: %p\n",s);    //常量字符串
  printf("string addr: %p\n","hhh");//同样是“hhh”看看会不会再开一个间还是再用上面那个空间
  printf("uninit addr: %p\n",&g_val1);//未初始化
  printf("init addr: %p\n",&g_val2);//已初始化
  
  char* heap = (char*)malloc(sizeof(char)*10);//开辟十个字节
  printf("heap addr: %p\n",heap);    //开辟空间的地址
  printf("stack addr: %p\n",&s);      //s就是在栈上面开辟的，所以直接用他的地址                


*/







  //printf("hhh->%s\n",getenv("hhh"));
//  printf("i am process   pid:%d   pidd:%d\n",getpid(),getppid());
  /*printf("PATH: %s\n",getenv("PATH"));
  printf("HOME; %s\n",getenv("HOME"));
  printf("SDHELL: %s\n",getenv("SHELL"));
*/
 /* extern char** environ;
  for(int i = 0;environ[i];++i)
  {
    //打印出系统环境变量
    printf("%d -> %s\n",i,environ[i]);
  }*/
  /*if(argc!=2)
  {
    printf("usage  -[a|h]\n");
    return 0;
  }
  if(!strcmp(argv[1],"-h"))
  {
    printf("Hello hhhh\n");
  }
  else if(!strcmp(argv[1],"-a"))
  {
    printf("Hello aaaa\n");
  }
  else{
   printf("Hello zzy\n"); 
  }*/
  /*for(int i = 0;i<argc;++i)
  {
    printf("argv[%d] -> %s\n",i,argv[i]);
  }*/
 /* int cnt  = 5;
  while(cnt){
     //printf("I am process   pid:%d   ppid:%d\n",getpid(),getppid());
     sleep(1);
     printf("I am  cmd -> process\n");
     --cnt;
  }
     return 0;
}*/
